package cn.tedu.eb_film.controller;

import cn.tedu.eb_film.pojo.dto.*;
import cn.tedu.eb_film.pojo.vo.MovieListVO;
import cn.tedu.eb_film.pojo.vo.MovieStandardVO;
import cn.tedu.eb_film.pojo.vo.MovieTypeListVO;
import cn.tedu.eb_film.pojo.vo.MovieTypeStandardVO;
import cn.tedu.eb_film.service.MovieService;
import cn.tedu.eb_film.service.MovieTypeService;
import cn.tedu.eb_film.utils.GeneralListener;
import cn.tedu.eb_film.utils.JsonResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 电影类型表的控制层
 * @author diaojie
 * @since 2023/03/29 20:24:07
 */
@RestController
@Slf4j
@Validated
@Api(tags = "电影类型表管理")
@RequestMapping("/movieTypes")
public class MovieTypeController {

    @Autowired
    private MovieTypeService movieTypeService;

    @PostMapping("/addNew")
    @ApiOperation("添加电影类型")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid MovieTypeAddDTO movieTypeAddDTO){
        log.debug("开始处理【添加电影类型】请求，参数：{}",movieTypeAddDTO);
        movieTypeService.addNew(movieTypeAddDTO);
        return JsonResult.ok();
    }

    @RequestMapping("/insertBash")
    @ApiOperation("添加电影类型s")
    @ApiOperationSupport(order = 110)
    public JsonResult<Void> insertBash(MultipartFile file) throws IOException {
        log.debug("开始处理【添加电影类型】请求");
        String path = "D:\\ebType.xlsx";
//        1、使用实现ReadListener的方式
//        EasyExcel.read(file.getInputStream(),CourseInfo.class, new UploadDataListener(courseInfoMapper)).sheet().doRead();
//        2、使用继承AnalysisEventListener的方式
//        EasyExcel.read(file.getInputStream(), CourseInfo.class,new CourseInfoListener(courseService)).sheet().doRead();
//        3、使用继承AnalysisEventListener且使用泛型的方式：
//        3.1、建立一个通用的读取监听器，在该监听器读取到数据，并使用List保存，读取完成之后，使用getList()方法获得
        GeneralListener<MovieTypeAddDTO> generalListener = new GeneralListener<>();
//        3.2、执行Excel的读取，.read()方法需要传入三个参数：1、文件。2、实体类。3、监听器。
        EasyExcel.read(file.getInputStream(),MovieTypeAddDTO.class,generalListener).sheet().doRead();
        log.info("读取到的数据大小为:{}", JSON.toJSONString(generalListener.getList().size()));
        //        3.3、获得监听器读取的数据列表
        List<MovieTypeAddDTO> movieTypeAddDTOList = generalListener.getList();
        for (MovieTypeAddDTO movieExcelDTO : movieTypeAddDTOList) {
            log.debug("................................");
            log.debug(".............movieExcelDTO:{}",movieExcelDTO);
        }
//        3.4、执行后续入库操作
        movieTypeService.insetBash(movieTypeAddDTOList);

        return JsonResult.ok();
    }



    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除电影类型")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "电影类型ID", required = true, dataType = "int")
    })
    public JsonResult<Void> deleteById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Integer id){

        log.debug("开始处理【删除电影类型】请求，参数：{}",id);
        movieTypeService.deleteById(id);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据id修改电影院类型数据")
    @ApiOperationSupport(order = 210)
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Integer id,
                                           @Valid MovieTypeUpdateDTO movieTypeUpdateDTO){

        log.debug("开始处理【修改电影类型】请求，参数id：{},参数：{}",id,movieTypeUpdateDTO);
        movieTypeService.updateInfoById(id, movieTypeUpdateDTO);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 220)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "int")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Integer id) {
        log.debug("开始处理【启用电影类型】的请求，参数：{}", id);
        movieTypeService.setEnable(id);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 221)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "int")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Integer id) {
        log.debug("开始处理【禁用电影类型】的请求，参数：{}", id);
        movieTypeService.setDisable(id);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询电影院类型数据")
    @ApiOperationSupport(order = 210)
    public JsonResult<MovieTypeStandardVO> getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！")  Integer id){
        MovieTypeStandardVO movieType = movieTypeService.getStandardById(id);
        return JsonResult.ok(movieType);
    }

    @GetMapping("/list")
    @ApiOperation("电影类型列表")
    @ApiOperationSupport(order = 300)
    public JsonResult<List<MovieTypeListVO>> list(){
        log.debug("开始处理【电影类型列表】请求，无参数");
        List<MovieTypeListVO> movieTypeList = movieTypeService.list();
        return JsonResult.ok(movieTypeList);
    }


    @PostMapping("/listSearch")
    @ApiOperation("按照搜索条件查询电影类型列表")
    @ApiOperationSupport(order = 310)
    public JsonResult<List<MovieTypeListVO>> listSearch(String name){
        log.debug("开始处理【按照搜索条件查询电影类型列表】请求，name:{}",name);
        List<MovieTypeListVO> movieTypeListSearch = movieTypeService.listSearch(name);
        return JsonResult.ok(movieTypeListSearch);
    }
}
